<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/2 0002
 * Time: 9:50
 */
namespace fast;
// 引入依赖库
require '../../vendor/autoload.php';

// 声明命名空间
use obs\ObsClient;
use think\Config;

class Obs{
    protected $config = [];

    protected $obsClient = null;

    protected $key = null;

    protected $secret = null;

    protected $bucket= null;

    protected $baseData = [];
    public function __construct()
    {
        $this->config = Config::get('obs');
        $this->key = $this->config['key'];
        $this->secret = $this->config['secret'];
        $this->bucket = $this->config['bucket'];
        $this->endpoint = $this->config['endpoint'];
        $this->obsClient = new ObsClient([
            'key' => $this->key,
            'secret' => $this->secret,
            'endpoint' => $this->endpoint,
        ]);
    }

    public function __destruct()
    {
        // 关闭obsClient
        $this->obsClient -> close();
    }

    public function createBucket(){
        $res = $this->obsClient->createBucket([
            'Bucket' => $this->bucket
        ]);
        return $res;
    }

    public function put($type=1){
        $data = [
            'Bucket' => $this->bucket,
            'Key' => $this->key,
        ];
        if($type==1){
            $data['SourceFile'] = 'localfile';
        }elseif ($type==0){
            $data['body'] = 'hello bos';
        }
        $res = $this->obsClient->putObject($data);
        return $res;
    }

    public function get(){
        $data = [
            'Bucket' => $this->bucket,
            'Key' => $this->key,
        ];
        $res = $this->obsClient->getObject($data);
        return $res;
    }

    public function delete(){
        $data = [
            'Bucket' => $this->bucket,
            'Key' => $this->key,
        ];
        $res = $this->obsClient->deleteObject($data);
        return $res;
    }



}